package com.student_system.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.student_system.common.Result;
import com.student_system.system.entity.SysRole;
import com.student_system.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljy
 * @since 2025-05-14
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    ISysRoleService sysRoleService;

    /**
     * 添加角色信息
     *
     * @param sysRole SysRole
     * @return String
     */
    @PostMapping("/addRole")
    public String addRole(@RequestBody SysRole sysRole) {
        //业务代码需要写在service层
        sysRoleService.addRole(sysRole);//相当于写了一条SQL insert语句插入了一条数据
        return "success addRole";
    }

    /**
     * 获取所有角色信息
     * @return Result.success(roleList) 角色列表
     */
    @GetMapping("/getAllRole")
    public Result.ResultData<List<SysRole>> getAllRoles(){
        List<SysRole> roles = sysRoleService.list();
        return Result.success(roles);
    }
    /**
     * 删除角色信息
     *
     * @param RoleId Integer
     * @return String
     */
    @DeleteMapping("/deleteRole/{RoleId}")
    public String deleteRole(@PathVariable Integer RoleId) {
        sysRoleService.removeById(RoleId);
        return "success deleteRole";
    }

    /**
     * 修改角色信息
     *
     * @param sysRole SysRole
     * @return String
     */
    @PutMapping("/updateRole")
    public String updateRole(@RequestBody SysRole sysRole) {
        sysRoleService.updateById(sysRole);
        return "success updateRole";
    }


    /**
     * 查询角色信息
     *
     * @param RoleId Integer
     * @return SysRole
     */
    @GetMapping("/getRole/{RoleId}")
    public SysRole getRole(@PathVariable Integer RoleId) {
        return sysRoleService.getById(RoleId);
    }


    /**
     * 分页查询角色信息
     *
     * @param roleName String
     * @param current  Integer
     * @param size Integer
     */
    @GetMapping("/pageRole")
    public Page<SysRole> pageRole(@RequestParam(required = false) String roleName,
                                  @RequestParam(required = false) Integer current,
                                  @RequestParam(required = false) Integer size) {
        //TODO 分页查询角色信息
        if (current == null) {
            current = 1;
        }
        if (size == null) {
            size = 5;
        }
        Page<SysRole> page = new Page<>(current, size);
        Page<SysRole> sysRolePage = sysRoleService.queryRolePage(page, roleName);
        return sysRolePage;
    }
}
